package org.csu.petserver.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.petserver.domain.Item;
import org.csu.petserver.domain.LineItem;
import org.csu.petserver.domain.Order;
import org.csu.petserver.persistence.ItemMapper;
import org.csu.petserver.persistence.LineItemMapper;
import org.csu.petserver.persistence.OrderMapper;
import org.csu.petserver.service.LineItemInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Repository("LineItemInfo")
@MapperScan("org.csu.petserver.persistence")
public class LineItemInfoImpl implements LineItemInfo{
    @Autowired
    private LineItemMapper lineItemMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Override
    public boolean addLineItemInCertainOrder(Order order, LineItem lineItem) {
        QueryWrapper<LineItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemid",lineItem.getItemid()).eq("orderid",order.getId());
        if(lineItemMapper.selectOne(queryWrapper) != null){
            return false;
        }
        if(orderMapper.selectById(order.getId()) == null){
            return false;
        }
        QueryWrapper<LineItem> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("orderid",order.getId());
        List<LineItem> lineItems = lineItemMapper.selectList(queryWrapper1);
        lineItem.setOrderid(order.getId());
        lineItem.setQuantity(1);
        if(lineItems == null){
            lineItem.setLinenum(1);
        }
        else {
            lineItem.setLinenum(lineItems.size()+1);
        }
        lineItemMapper.insert(lineItem);
        return true;
    }

    @Override
    public boolean deleteLineItem(LineItem lineItem) {
        QueryWrapper<LineItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",lineItem.getOrderid()).eq("linenum",lineItem.getLinenum());
        lineItemMapper.delete(queryWrapper);
        return true;
    }

    @Override
    public boolean updateQuantity(LineItem lineItem) {
        QueryWrapper<LineItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",lineItem.getOrderid()).eq("linenum",lineItem.getLinenum());
        lineItemMapper.update(lineItem,queryWrapper);
        return true;
    }
    public List<Item> getTopEightBestSellingItems(){
        List<LineItem> lineItems = lineItemMapper.selectList(null);
        List<Item> items = new ArrayList<>();
        List<Integer> numbers = new ArrayList<>();
        List<Item> newItems = new ArrayList<>();
        int index = 0;
        int max = 0;
        for (LineItem lineItem:lineItems) {
            QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("itemid",lineItem.getItemid());
            Item item1 = itemMapper.selectOne(queryWrapper);
            if(item1 != null){
                if(items.contains(item1)){
                    index = items.indexOf(item1);
                    int quantity = numbers.get(index) + lineItem.getQuantity();
                    numbers.set(index,quantity);
                }
                else {
                    items.add(item1);
                    numbers.add(lineItem.getQuantity());
                }
            }
        }
        for(int i=0 ; i<8 ; i++){
            int indexOfMax = numbers.indexOf(Collections.max(numbers));
            numbers.set(indexOfMax,0);
            newItems.add(items.get(indexOfMax));
        }
        return newItems;
    }
    public List<Integer> getTopEightBestSellingItemsQuantity(List<Item> items){
        List<Integer> numbers = new ArrayList<>();
        for (Item item: items) {
            int quantity = 0;
            QueryWrapper<LineItem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("itemid",item.getId());
            List<LineItem> lineItems = lineItemMapper.selectList(queryWrapper);
            for (LineItem lineItem:lineItems) {
                quantity+=lineItem.getQuantity();
            }
            numbers.add(quantity);
        }
        return numbers;
    }
}
